package org.linlinjava.litemall.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.vo.AdminLumiereAbnormalSettlementVO;
import org.linlinjava.litemall.admin.vo.AdminlumiereCalculateSettlementVO;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallShop;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LumiereAbnormalSettlement;
import org.linlinjava.litemall.db.domain.LumiereCalculateSettlement;
import org.linlinjava.litemall.db.service.LitemallShopService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.LumiereAbnormalSettlementService;
import org.linlinjava.litemall.db.service.LumiereCalculateSettlementService;
import org.linlinjava.litemall.db.util.PageTools;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 下班、日中结算控制器
 *
 * @author macmini
 * @date 2022/12/07
 */
@Api(tags = "下班、日中结算API")
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/settlement")
public class AdminLumiereSettlementController {

    private final LumiereCalculateSettlementService lumiereCalculateSettlementService;

    private final LumiereAbnormalSettlementService lumiereAbnormalSettlementService;

    private final LitemallShopService litemallShopService;

    private final LitemallUserService litemallUserService;


    @ApiOperation("下班结算数")
    @GetMapping("calculateSettlementSize")
    @LogAnno
    public ResponseUtil.Response calculateSettlementSize(@RequestParam("shopId")Integer shopId){
        if(ObjectUtil.isNull(shopId)){return ResponseUtil.ok(0);}
        return ResponseUtil.ok(lumiereCalculateSettlementService.list(Wrappers.lambdaQuery(LumiereCalculateSettlement.class)
                .eq(LumiereCalculateSettlement::getShopId,shopId)
                .eq(LumiereCalculateSettlement::getDeleted,0)).size());
    }

    /**
     * 下班结算列表
     *
     * @param page  页面
     * @param limit 限制
     * @return {@link ResponseUtil.Response}
     */
    @ApiOperation("下班结算列表")
    @GetMapping("calculateSettlementList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "page",defaultValue = "1"),
            @ApiImplicitParam(name = "limit",value = "limit",defaultValue = "40"),
            @ApiImplicitParam(name = "shopId",value = "门店ID")
    })
    @LogAnno
    public ResponseUtil.Response calculateSettlementList(
            @RequestParam("page")Integer page,
            @RequestParam("limit")Integer limit,
            Integer shopId){
        List<AdminlumiereCalculateSettlementVO> adminlumiereCalculateSettlementVOS = lumiereCalculateSettlementService.list(Wrappers.lambdaQuery(LumiereCalculateSettlement.class)
                .eq(shopId!=null,LumiereCalculateSettlement::getShopId,shopId)
                .orderByDesc(LumiereCalculateSettlement::getAddTime)
        ).stream().map(lumiereCalculateSettlement -> {
            LitemallShop litemallShop = litemallShopService.findById(lumiereCalculateSettlement.getShopId());
            LitemallUser litemallUser = litemallUserService.findById(lumiereCalculateSettlement.getUserId());
            AdminlumiereCalculateSettlementVO adminlumiereCalculateSettlementVO =AdminlumiereCalculateSettlementVO.builder()
                    .shopName(litemallShop.getName())
                    .userName(litemallUser.getUsername())
                    .build();
            BeanUtils.copyProperties(lumiereCalculateSettlement,adminlumiereCalculateSettlementVO);
            return adminlumiereCalculateSettlementVO;
        }).collect(Collectors.toList());
        PageTools pageTools = PageTools.builder().build();
        pageTools.pageTools(page,limit,adminlumiereCalculateSettlementVOS);
        return ResponseUtil.ok(pageTools);
    }


    /**
     * 日中结算列表
     *
     * @param page  页面
     * @param limit 限制
     * @return {@link ResponseUtil.Response}
     */
    @ApiOperation("日中结算列表")
    @GetMapping("abnormalSettlement")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "page",defaultValue = "1"),
            @ApiImplicitParam(name = "limit",value = "limit",defaultValue = "40"),
            @ApiImplicitParam(name = "shopId",value = "门店ID")
    })
    public ResponseUtil.Response abnormalSettlement(
            @RequestParam("page")Integer page,
            @RequestParam("limit")Integer limit,
            Integer shopId){
        List<AdminLumiereAbnormalSettlementVO> adminLumiereAbnormalSettlementVOS = lumiereAbnormalSettlementService.list(Wrappers.lambdaQuery(LumiereAbnormalSettlement.class)
                .eq(shopId!=null,LumiereAbnormalSettlement::getShopId,shopId)
                .orderByDesc(LumiereAbnormalSettlement::getAddTime)
        ).stream().map(lumiereAbnormalSettlement -> {
            LitemallShop litemallShop = litemallShopService.findById(lumiereAbnormalSettlement.getShopId());
            LitemallUser litemallUser = litemallUserService.findById(lumiereAbnormalSettlement.getUserId());
            AdminLumiereAbnormalSettlementVO adminLumiereAbnormalSettlementVO = AdminLumiereAbnormalSettlementVO.builder()
                    .shopName(litemallShop.getName())
                    .userName(litemallUser.getUsername())
                    .build();
            BeanUtils.copyProperties(lumiereAbnormalSettlement,adminLumiereAbnormalSettlementVO);
            return adminLumiereAbnormalSettlementVO;
        }).collect(Collectors.toList());
        PageTools pageTools = PageTools.builder().build();
        pageTools.pageTools(page,limit,adminLumiereAbnormalSettlementVOS);
        return ResponseUtil.ok(pageTools);
    }



    @ApiOperation("日中结算数")
    @GetMapping("abnormalSettlementSize")
    @LogAnno
    public ResponseUtil.Response abnormalSettlementSize(@RequestParam("shopId") Integer shopId){
        if(ObjectUtil.isNull(shopId)){return ResponseUtil.ok(0);}
        return ResponseUtil.ok(lumiereAbnormalSettlementService.list(Wrappers.lambdaQuery(LumiereAbnormalSettlement.class)
                .eq(LumiereAbnormalSettlement::getShopId,shopId)
                .eq(LumiereAbnormalSettlement::getDeleted,0)).size());
    }





}
